import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)

// const ErrorPage = () => import(/*webpackChunkName: 'pack4'*/ './components/ErrorPage.vue')
const ErrorPage = () => import('./components/ErrorPage.vue')
const PageLayout = () => import('./layout/PageLayout.vue')
const Login = () => import('./page/Login.vue')
const Welcome = () => import('./page/Welcome.vue')
const SysUserPage = () => import('./page/sys_user/SysUserPage.vue')
const SysRolePage = () => import('./page/sys_role/SysRolePage.vue')
const SysDeptPage = () => import('./page/sys_dept/SysDeptPage.vue')
const SysApplicationPage = () => import('./page/sys_application/SysApplicationPage.vue')
const SysLogPage = () => import('./page/sys_log/SysLogPage.vue')
const SysResourcePage = () => import('./page/sys_resource/SysResourcePage.vue')

import auth_api from './api/auth_api'

const routes = [
    {
        path: '/',
        component: PageLayout,
        beforeEnter: (to, from, next) => {
            beforePageEnter(to, from, next)
        },
        children: [
            {
                path: '/',
                component: Welcome
            },
            {
                path: '/welcome',
                component: Welcome,
                meta: {
                    title: '欢迎'
                }
            },
            {
                path: '/user',
                component: SysUserPage,
                meta: {
                    title: '用户管理'
                }
            },
            {
                path: '/dept',
                component: SysDeptPage,
                meta: {
                    title: '组织管理'
                }
            },
            {
                path: '/role',
                component: SysRolePage,
                meta: {
                    title: '角色管理'
                }
            },
            {
                path: '/app',
                component: SysApplicationPage,
                meta: {
                    title: '系统应用'
                }
            },
            {
                path: '/resource',
                component: SysResourcePage,
                meta: {
                    title: '资源列表'
                }
            },
            {
                path: '/log',
                component: SysLogPage,
                meta: {
                    title: '日志管理'
                }
            },
        ]
    },
    {
        path: '/login',
        component: Login,
        beforeEnter: (to, from, next) => {
            beforePageEnter(to, from, next)
        },
        meta: {
            title: '登录'
        }
    },
    {
        path: '*',
        component: ErrorPage,
        meta: {
            title: '页面不存在'
        }
    },
]

const loginUrl = '/login'

const beforePageEnter = (to, from, next) => {
    authCheck(to.path, next)
}

const getQueryVariable = variable => {
    var query = window.location.search.substring(1);
    var vars = query.split("&");
    for (var i = 0; i < vars.length; i++) {
        var pair = vars[i].split("=");
        if (pair[0] == variable) { return pair[1]; }
    }
    return '';
}

const authCheck = (path, cb) => {
    const token_enc = getQueryVariable('token_enc')
    auth_api.userInfo(token_enc).then(data => {
        window.$vm.$store.dispatch('auth/setUserInfo', data)
        if (data.id != null || path == loginUrl) {
            cb()
        } else {
            cb({
                path: loginUrl,
                replace: true
            })
        }
    }).catch(err => {
        if (err.message) {
            window.$vm.$Message.error({
                background: true,
                content: err.message
            })
        }
    })
}

const router = new VueRouter({
    mode: 'history',
    routes
})

router.beforeEach((to, from, next) => {
    if (to.meta && to.meta.title) {
        document.title = to.meta.title + ' - 统一用户管理平台'
    }
    next()
})
export default router
